/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.postp.postp_view.command;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.view.ExtensionFileFilter;
import br.ufmg.dees.insane.commons.view.ModelList;
import br.ufmg.dees.insane.model.femModel.FemModel;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.parser.FemModelToPostpModel;
import br.ufmg.dees.insane.persistence.PersistenceAsXML;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;

/**
 * This class is an implementation of the interface Command.
 * It's unabled to undo or redo.
 * Its "execute" method fills the current fem model with
 * the data from a XML file in a given file.
 *
 * @author Penna, Samuel & Fonseca, Flavio & Pitangueira, Roque
 * @since 05/07/2006 2004
 * @see gui.command.Command
 */
public class ImportFemXMLCommand implements Command {
    
    private DesktopPostp parent;
    private JFileChooser fileChooser = new JFileChooser();
    private ExtensionFileFilter filter = new ExtensionFileFilter(new String[]{"xml"}, "XML Files");
    private PostpModel postpModel;
    private FemModelToPostpModel parser;
	
//*****************************************************************************
	
    /**
     * Creates new ImportXMLCommand
     * and sets the FileFilter of FileChooser.
     *
     * @param parent The JFrame parent.
     */
    public ImportFemXMLCommand(JFrame parent) {
        this.parent = (DesktopPostp)parent;
        fileChooser.setFileFilter(filter);
    }
    
//*****************************************************************************
	
    /**
     * Calls the file chooser an encode the fem model as a XML file.
     *
     * @see gui.command.Command#execute()
     */
    public void execute() {
		fileChooser.setDialogTitle("Import XML");
		int retVal = fileChooser.showDialog(parent, "Import");
        if (retVal == JFileChooser.APPROVE_OPTION) {
        	//Getting the file name
            String s = fileChooser.getSelectedFile().getName();
            
			//Getting the file extension
            String ext = new String();
            int i = s.lastIndexOf('.');
            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            
			//Comparing the extension
            if (!ext.equals("xml")) {
                s = s + ".xml";
                File file = fileChooser.getSelectedFile();
                
				//Changing the file in case of no good extensions
                fileChooser.setSelectedFile(new File(file.getParent() + File.separator + s));
            }
			
            parent.setInterface();
	 	    
            PersistenceAsXML p = new PersistenceAsXML(); 
            p.fillInsaneFromFile(fileChooser.getSelectedFile()+"");
            
            parser = new FemModelToPostpModel((FemModel)p.getModel());
           // parser.setSolution(p.getSolution());
            
	 	    postpModel = new PostpModel();
	 	    postpModel.addStep(parser.getStepPostp());
	 	    postpModel.setLabel(fileChooser.getSelectedFile().getName().replaceAll(".xml",""));
	 	    if(((ModelList)this.parent.getModelList()).getModelList().containsKey(postpModel.getLabel())){
	 	    	this.parent.getModelList().removeModel(postpModel.getLabel());
	 	    	this.parent.getModelList().putModel(postpModel.getLabel(),postpModel);
	 	  
	 	    	InterfacePostp intFrame = new InterfacePostp();
	 	    	intFrame.validate();
	 	    	intFrame.setPostpModel(postpModel);
	 	    	intFrame.setModel(parser.getStepPostp());
	 	    	intFrame.setTitle(postpModel.getLabel());
	 	    	(this.parent).addInternalFrame(intFrame);
	 	    	intFrame.toFront();

	 	    	String message = "A model with same name was found and overrided!"; 
	 	    	JOptionPane.showMessageDialog(parent, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
	 	    }else{
	 	    	postpModel.setLabel(fileChooser.getSelectedFile().getName().replaceAll(".ipp",""));
	 	    	this.parent.getModelList().putModel(postpModel.getLabel(),postpModel);
	 	    	InterfacePostp intFrame = new InterfacePostp();
	 		 	intFrame.validate();
	 		 	intFrame.setPostpModel(postpModel);
	 		 	intFrame.setModel((StepPostp)postpModel.getStepList().get(0));
	 		 	intFrame.setTitle(postpModel.getLabel());
	 		 	(this.parent).addInternalFrame(intFrame);
	 		 	intFrame.toFront();
	         }
	 	   
	 	    
        }
    }
    
//*****************************************************************************
    /**
     * Impossible to undo - not implemented.
     */
    public void undo() {}
    
//*****************************************************************************
	
    /**
     * Impossible to redo - not implemented.
     */
    public void redo() {}

//*****************************************************************************
}
